package cj.web.shop.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.admin.dao.ConfigInfoDAO;
import cj.web.admin.dao.GoodsDAO;
import cj.web.admin.domain.ConfigInfo;
import cj.web.admin.domain.Goods;
import cj.web.admin.utils.Base64;
import cj.web.admin.utils.Page;
import cj.web.admin.utils.ReturnUUID;
import cj.web.login.controllers.BaseController;

@Path("")
public class PortalController extends BaseController{

	protected static final Log logger = LogFactory.getLog(PortalController.class); 
	
	@Autowired
	private GoodsDAO goodsDao;
	
	@Autowired
	private ConfigInfoDAO configInfoDao;
	
	/**
	 * 首页
	 * @param model
	 * @param goods
	 * @return
	 */
	@Get("")
    public String index(Model model,Goods goods) {
		
		int count = goodsDao.querySize(goods);
		goods.setStart(Integer.valueOf(ReturnUUID.getRandomNum(count-100, 0)));
		goods.setLength(44);
		
		List<Goods> objs = goodsDao.query(goods);
		
		for(Goods obj:objs){
			obj.setImages(Base64.getFromBase64(obj.getImages()));
			obj.setDetailUrl(Base64.getFromBase64(obj.getDetailUrl()));
			obj.setTkUrl(Base64.getFromBase64(obj.getTkUrl()));
			obj.setGoodsCouponUrl(Base64.getFromBase64(obj.getGoodsCouponUrl()));
		}
		
		model.add("objs",objs);
		
		List<ConfigInfo> configList = configInfoDao.queryByCode("banner");
		model.add("topImagelis",configList);
		
        return "/views/portal/index.vm";
    }
	
	/**
	 * 查询页面
	 * @param model
	 * @param goods
	 * @return
	 */
	@Get("query")
    public String query(Model model,Goods goods) {
		
		model.add("category",goods.getCategory()==null?"":goods.getCategory());
		model.add("order",goods.getOrder()==null?"":goods.getOrder());
		model.add("name",goods.getName()==null?"":goods.getName());
		
        return "/views/portal/query.vm";
    }
	
	/**
	 * pc版商城订单分页
	 * @param start
	 * @param length
	 * @param request
	 * @return
	 */
	@Post("pcOrlis")
	public String getOrderslis(@Param("start") int start,@Param("length") int length,
			Goods goods, HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		goods.setStart(start*length);
		goods.setLength(length);
		List<Goods> objLis = goodsDao.query(goods);
		for(Goods obj:objLis){
			obj.setImages(Base64.getFromBase64(obj.getImages()));
			obj.setDetailUrl(Base64.getFromBase64(obj.getDetailUrl()));
			obj.setTkUrl(Base64.getFromBase64(obj.getTkUrl()));
			obj.setGoodsCouponUrl(Base64.getFromBase64(obj.getGoodsCouponUrl()));
		}
		int totalCount = goodsDao.querySize(goods);
		
		//分页计算
		Page p= new Page<Goods>(0, length, objLis, totalCount);
		
		jo.put("list", objLis);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		return "@"+jo.toString();
	}
	
	/**
	 * 详情
	 * @param model
	 * @param id
	 * @return
	 */
	@Get("detail/{id:[0-9]+}")
    public String detail(Model model,@Param("id") String id) {
		Goods obj = goodsDao.queryById(id);
		
		obj.setImages(Base64.getFromBase64(obj.getImages()));
		obj.setDetailUrl(Base64.getFromBase64(obj.getDetailUrl()));
		obj.setTkUrl(Base64.getFromBase64(obj.getTkUrl()));
		obj.setGoodsCouponUrl(Base64.getFromBase64(obj.getGoodsCouponUrl()));
		
		model.add("obj",obj);
		
		Goods goods = new Goods();
		goods.setStart(0);
		goods.setLength(4);
		goods.setCategory(obj.getCategory().split("/")[0]);
		List<Goods> objs = goodsDao.query(goods);
		
		for(Goods good:objs){
			good.setImages(Base64.getFromBase64(good.getImages()));
			good.setDetailUrl(Base64.getFromBase64(good.getDetailUrl()));
			good.setTkUrl(Base64.getFromBase64(good.getTkUrl()));
			good.setGoodsCouponUrl(Base64.getFromBase64(good.getGoodsCouponUrl()));
		}
		
		model.add("objs",objs);
		
        return "/views/portal/detail.vm";
    }
	
	
	/**
	 * 9块9包邮查询页面
	 * @param model
	 * @param goods
	 * @return
	 */
	@Get("queryNine")
    public String queryNine(Model model,Goods goods) {
		
		model.add("category",goods.getCategory()==null?"":goods.getCategory());
		model.add("order",goods.getOrder()==null?"":goods.getOrder());
		model.add("name",goods.getName()==null?"":goods.getName());
		
        return "/views/portal/nine.vm";
    }
	
	/**
	 * 9块9包邮pc版商城订单分页
	 * @param start
	 * @param length
	 * @param request
	 * @return
	 */
	@Post("pcOrNinelis")
	public String getNineOrderslis(@Param("start") int start,@Param("length") int length,
			Goods goods, HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		goods.setStart(start*length);
		goods.setLength(length);
		List<Goods> objLis = goodsDao.queryNine(goods);
		for(Goods obj:objLis){
			obj.setImages(Base64.getFromBase64(obj.getImages()));
			obj.setDetailUrl(Base64.getFromBase64(obj.getDetailUrl()));
			obj.setTkUrl(Base64.getFromBase64(obj.getTkUrl()));
			obj.setGoodsCouponUrl(Base64.getFromBase64(obj.getGoodsCouponUrl()));
		}
		int totalCount = goodsDao.queryNineSize(goods);
		
		//分页计算
		Page p= new Page<Goods>(0, length, objLis, totalCount);
		
		jo.put("list", objLis);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		return "@"+jo.toString();
	}
}
